package com.qingge.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.entity.Flower;
import com.qingge.springboot.mapper.FlowerMapper;
import com.qingge.springboot.mapper.GwcMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingge.springboot.common.Result;

import com.qingge.springboot.service.IGwcService;
import com.qingge.springboot.entity.Gwc;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 格温不受影响
 * @since 2022-05-02
 */
@RestController
@RequestMapping("/gwc")
public class GwcController {

    @Resource
    private IGwcService gwcService;

    @Autowired
    private GwcMapper gwcMapper;


    @Autowired
    private FlowerMapper flowerMapper;

    @GetMapping("/updateCnt")
    public Result updateCnt(@RequestParam Integer gwcId,@RequestParam Integer cnt, @RequestParam Integer fid) {
        Gwc gwc = new Gwc();
        gwc.setCnt(cnt);
        gwc.setGwcId(gwcId);
        // 是否大于库存
        Flower flower = flowerMapper.selectById(fid);
        if (gwc.getCnt() > flower.getCnt()) {
            gwc.setCnt(flower.getCnt());
        }
        gwcMapper.updateById(gwc);
        return Result.success();
    }


    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Gwc gwc) {
        Integer cnt = gwc.getCnt();
        if (gwc.getUid() == null || gwc.getFid() == null || cnt == null) {
            return Result.error("所需信息不能为空");
        }
        if (cnt <= 0) {
            return Result.error("数量必须大于0");
        }
        QueryWrapper<Gwc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", gwc.getUid()).
                eq("fid", gwc.getFid());
        Gwc one = gwcMapper.selectOne(queryWrapper);
        if (one != null) {
            one.setCnt(one.getCnt() + cnt);
            gwcMapper.updateById(one);
        } else {
            gwcMapper.insert(gwc);
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        gwcService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        gwcService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(gwcService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(gwcService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                           @RequestParam Integer uid) {
        QueryWrapper<Gwc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid)
                .orderByDesc("gwc_id");
        return Result.success(gwcService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

